package com.zzyl.service.impl;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.PendingTasksDto;
import com.zzyl.entity.CheckIn;
import com.zzyl.mapper.CheckInMapper;
import com.zzyl.service.PendingTaskService;
import com.zzyl.service.WorkFlowService;
import com.zzyl.vo.PendingTaskVo;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PendingTaskServiceImpl implements PendingTaskService {

    @Resource
    WorkFlowService workFlowService;

    @Resource
    CheckInMapper checkInMapper;

    @Override
    public ResponseResult<PageResponse<PendingTaskVo>> selectByPage(PendingTasksDto dto) {
        //查询待办任务
        if (dto.getIsHandle().equals(0)) {
            List<Task> tasks = workFlowService.queryTaskByPage(dto.getPageNum()
                    , dto.getPageSize(),dto.getAssigneeId().toString());
            //做实体类的转换
            List<PendingTaskVo> collect = tasks.stream().map(task -> {
                PendingTaskVo pendingTaskVo = new PendingTaskVo();
                pendingTaskVo.setId(task.getId());
                pendingTaskVo.setAssigneeId(task.getAssignee());
                pendingTaskVo.setCheckInId(task.getBusinessKey());
                //剩余的字段返回连表查询获得
                CheckIn checkIn = checkInMapper.queryById(Long.valueOf(task.getBusinessKey()));
                pendingTaskVo.setCode(checkIn.getCheckInCode());
                pendingTaskVo.setTitle(checkIn.getTitle());
                pendingTaskVo.setApplicat(checkIn.getApplicat());
                pendingTaskVo.setApplicationTime(checkIn.getCreateTime());
                pendingTaskVo.setStatus(checkIn.getStatus());
                return pendingTaskVo;
            }).collect(Collectors.toList());
            return ResponseResult.success(PageResponse.of(collect));
        } else {
            //查询已处理任务
            List<HistoricTaskInstance> historyTask = workFlowService
                    .queryHistoryTask(dto.getPageNum(),
                            dto.getPageSize(),dto.getAssigneeId().toString());
            List<PendingTaskVo> collect = historyTask.stream().map(task -> {
                PendingTaskVo pendingTaskVo = new PendingTaskVo();
                pendingTaskVo.setId(task.getId());
                pendingTaskVo.setAssigneeId(task.getAssignee());
                pendingTaskVo.setCheckInId(task.getBusinessKey());
                //剩余的字段返回连表查询获得
                CheckIn checkIn = checkInMapper.queryById(Long.valueOf(task.getBusinessKey()));
                pendingTaskVo.setCode(checkIn.getCheckInCode());
                pendingTaskVo.setTitle(checkIn.getTitle());
                pendingTaskVo.setApplicat(checkIn.getApplicat());
                pendingTaskVo.setApplicationTime(checkIn.getCreateTime());
                pendingTaskVo.setStatus(checkIn.getStatus());
                return pendingTaskVo;
            }).collect(Collectors.toList());
            return ResponseResult.success(PageResponse.of(collect));
        }

    }
}
